const StatisticalTableDataSer = require('../../service/StatisticalTableData/StatisticalTableDataSer')
const DataStatisticSer = require('../../service/dataStatistics/DataStatisticSer')
const {getTodayDate} = require('../../utils/getTodayDate')
const BlogCommentSet = require('../../service/blog/blogCommentSer')
const MessageBoardSer = require('../../service/messageBoardModel/MessageBoardModelSer')


class StatisticalTableDataCon {
    // 添加当前的访问数据
    async addData() {
        try {
            //获取当天访问量
            const today = getTodayDate() + ' 00:00:00'
            const todayData = await DataStatisticSer.getData(today)
            if (todayData.length === 0) return
            const ipList = []
            todayData.forEach(i => {
                if (i.ip_location) {
                    ipList.push(i.ip_location)
                }
            })
            const setIpList = new Set(ipList)

            //获取当前评论和留言量
            const startDate = getTodayDate() + ' 00:00:00'
            const endDate = getTodayDate() + ' 23:59:59'
            const CommentCount = await BlogCommentSet.getBlogCommentCount(startDate, endDate)
            const MessageCount = await MessageBoardSer.getMessageCount(startDate, endDate)
            const addData = {
                page_view: setIpList.size,
                comment_count: CommentCount.length + MessageCount.length,
                create_date: getTodayDate()
            }
            await StatisticalTableDataSer.addData(addData)
        } catch (e) {
            console.log(e)
        }
    }

    //获取访问数据
    async getDataPageViewCount(ctx) {
        try {
            const dateList = ctx.request.body
            let dataList = {
                pageView: [],
                commentCount: []
            }
            for (let i = 0; i < dateList.length; i++) {
                const searchData = await StatisticalTableDataSer.getDataPageViewCount(dateList[i])
                if (dateList.length !== 6) {
                    if (searchData.length === 0) {           //如果查询的数据为0
                        dataList.pageView.push(0)
                        dataList.commentCount.push(0)
                    } else {
                        dataList.pageView.push(searchData[0].page_view)
                        dataList.commentCount.push(searchData[0].comment_count)
                    }

                } else {
                    if (searchData.length === 0) {
                        dataList.pageView.push(0)
                        dataList.commentCount.push(0)
                    } else {
                        const pageViewCount = searchData.reduce((acc, cur) => {
                            const count = acc + cur.page_view
                            return count
                        }, 0)
                        const commentCount = searchData.reduce((acc, cur) => {
                            const count = acc + cur.comment_count
                            return count
                        }, 0)
                        dataList.pageView.push(pageViewCount)
                        dataList.commentCount.push(commentCount)
                    }
                }

                ctx.body = {
                    code: '200',
                    data: dataList
                }
            }
        } catch (e) {
            ctx.app.emit('error', e.toString(), ctx)
        }

    }
}

const a = new StatisticalTableDataCon()


a.addData()


module.exports = new StatisticalTableDataCon()
